/***************************************************
 *  blog.cc
 *
 *  Copyright (C) 2009  csip(amoblin@gmail.com)
 *
 *  ChangeLog:
 *
 *  Description:
 *	Warning: this file should be in UTF-8.
 *  
 ****************************************************/
#include "Mystar.h"

static int blogIsInitialized = 0;
static unsigned char m_IP[4];
static unsigned char m_NetMask[4];
static unsigned char m_NetGate[4];
static unsigned char m_DNS1[4];
static unsigned char circleCheck[2];	//那两个鬼值

//configure the 4 parameters Blog() and FillNetParameter() need.
void Mystar::InitializeBlog (const unsigned char *m_ip, const unsigned char *m_netmask,
        const unsigned char *m_netgate, const unsigned char *m_dns1)
{
  memcpy (m_IP, m_ip, 4);
  memcpy (m_NetMask, m_netmask, 4);
  memcpy (m_NetGate, m_netgate, 4);
  memcpy (m_DNS1, m_dns1, 4);

  Blog ();

  blogIsInitialized = 1;
}
//Fill in some additional information  Ruijie Corp. required.
//You should call InitializeBlog() before calling this function.
void Mystar::FillNetParamater (unsigned char ForFill[])
{
  if (blogIsInitialized == 0)
    err_quit ("Blog algorithm not initialied yet \n");

  ForFill[0] = Alog (m_IP[0]);
  ForFill[1] = Alog (m_IP[1]);
  ForFill[2] = Alog (m_IP[2]);
  ForFill[3] = Alog (m_IP[3]);
  ForFill[4] = Alog (m_NetMask[0]);
  ForFill[5] = Alog (m_NetMask[1]);
  ForFill[6] = Alog (m_NetMask[2]);
  ForFill[7] = Alog (m_NetMask[3]);
  ForFill[8] = Alog (m_NetGate[0]);
  ForFill[9] = Alog (m_NetGate[1]);
  ForFill[10] = Alog (m_NetGate[2]);
  ForFill[11] = Alog (m_NetGate[3]);
  ForFill[12] = Alog (m_DNS1[0]);
  ForFill[13] = Alog (m_DNS1[1]);
  ForFill[14] = Alog (m_DNS1[2]);
  ForFill[15] = Alog (m_DNS1[3]);
  ForFill[16] = Alog (circleCheck[0]);
  ForFill[17] = Alog (circleCheck[1]);
}
//A transformation of one-byte-for-one-byte
unsigned char Mystar::Alog (unsigned char BForAlog)
{
  int a = 0, b = 0, c = 0, d = 0, iRt;

  a = BForAlog;
  c = a;
  d = a;
  c &= 0x40;
  b = a;
  d >>= 2;
  c |= d;
  d = a;
  c >>= 2;
  d &= 0x20;
  c |= d;
  d = a;
  c >>= 2;
  d &= 0x10;
  c |= d;

  d = a;
  d &= 2;
  b <<= 2;
  d |= b;
  b = a;
  d <<= 2;
  b &= 4;
  a &= 8;
  d |= b;
  d <<= 2;
  d |= a;

  c >>= 1;
  d <<= 1;
  c |= d;
  iRt = (~c) & 0xff;
  return iRt;
}
//那帮家伙们，单靠这个算法就想区别实达客户端和非实达客户端 -_- !! 
//The only use of function Blog() is to work out circleCheck[2],
//with and only with the help of 4 parameters----m_IP, m_NetMask, m_NetGate, m_DNS1
void Mystar::Blog (void)
{
  static unsigned char Table[] = 
  {
0x00,0x00, 0x21, 0x10, 0x42, 0x20, 0x63, 0x30, 0x84, 0x40, 0xA5, 0x50, 0xC6, 0x60, 0xE7, 0x70,
0x08,0x81, 0x29, 0x91, 0x4A, 0xA1, 0x6B, 0xB1, 0x8C, 0xC1, 0xAD, 0xD1, 0xCE, 0xE1, 0xEF, 0xF1,
0x31,0x12, 0x10, 0x02, 0x73, 0x32, 0x52, 0x22, 0xB5, 0x52, 0x94, 0x42, 0xF7, 0x72, 0xD6, 0x62,
0x39,0x93, 0x18, 0x83, 0x7B, 0xB3, 0x5A, 0xA3, 0xBD, 0xD3, 0x9C, 0xC3, 0xFF, 0xF3, 0xDE, 0xE3,
0x62,0x24, 0x43, 0x34, 0x20, 0x04, 0x01, 0x14, 0xE6, 0x64, 0xC7, 0x74, 0xA4, 0x44, 0x85, 0x54,
0x6A,0xA5, 0x4B, 0xB5, 0x28, 0x85, 0x09, 0x95, 0xEE, 0xE5, 0xCF, 0xF5, 0xAC, 0xC5, 0x8D, 0xD5,
0x53,0x36, 0x72, 0x26, 0x11, 0x16, 0x30, 0x06, 0xD7, 0x76, 0xF6, 0x66, 0x95, 0x56, 0xB4, 0x46,
0x5B,0xB7, 0x7A, 0xA7, 0x19, 0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D, 0xCF, 0x7C, 0xDF, 0x9B, 0xAF, 0xBA, 0xBF, 0xD9, 0x8F, 0xF8, 0x9F,
0x17,0x6E, 0x36, 0x7E, 0x55, 0x4E, 0x74, 0x5E, 0x93, 0x2E, 0xB2, 0x3E, 0xD1, 0x0E, 0xF0, 0x1E
};
  static unsigned char sCircleBase[0x15] = 
  {
0x00,0x00, 0x13, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,0x00, 0x00, 0x00, 0x00
  };
  int iCircle = 0x15;
  int i, ax = 0, bx = 0, dx = 0;

  sCircleBase[0x05] = m_IP[0];
  sCircleBase[0x06] = m_IP[1];
  sCircleBase[0x07] = m_IP[2];
  sCircleBase[0x08] = m_IP[3];
  sCircleBase[0x09] = m_NetMask[0];
  sCircleBase[0x0a] = m_NetMask[1];
  sCircleBase[0x0b] = m_NetMask[2];
  sCircleBase[0x0c] = m_NetMask[3];
  sCircleBase[0x0d] = m_NetGate[0];
  sCircleBase[0x0e] = m_NetGate[1];
  sCircleBase[0x0f] = m_NetGate[2];
  sCircleBase[0x10] = m_NetGate[3];
  sCircleBase[0x11] = m_DNS1[0];
  sCircleBase[0x12] = m_DNS1[1];
  sCircleBase[0x13] = m_DNS1[2];
  sCircleBase[0x14] = m_DNS1[3];

  for (i = 0; i < iCircle; i++)
    {
      dx = ax;
      bx = 0;
      bx = (bx & 0xff00) | sCircleBase[i];	// add "( )" by cdx
      dx &= 0xffff;
      dx >>= 8;
      dx ^= bx;
      bx = 0;
      bx &= 0x00ff;
      bx |= (ax & 0xff) << 8;

      ax = Table[dx * 2] | Table[dx * 2 + 1] << 8;
      ax ^= bx;
    }
  circleCheck[0] = (unsigned char) ((ax & 0xff00) >> 8);
  circleCheck[1] = (unsigned char) (ax & 0x00ff);
}
